﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using PAT.Entities;
using PAT.Data;

#endregion

namespace PAT.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="PatEmpresaProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class PatEmpresaProviderBaseCore : EntityProviderBase<PAT.Entities.PatEmpresa, PAT.Entities.PatEmpresaKey>
	{		
		#region Get from Many To Many Relationship Functions
		#region GetByIdTallerFromPatEmpresaTaller
		
		/// <summary>
		///		Gets PAT_EMPRESA objects from the datasource by ID_TALLER in the
		///		PAT_EMPRESA_TALLER table. Table PAT_EMPRESA is related to table PAT_TALLER
		///		through the (M:N) relationship defined in the PAT_EMPRESA_TALLER table.
		/// </summary>
		/// <param name="_idTaller"></param>
		/// <returns>Returns a typed collection of PatEmpresa objects.</returns>
		public TList<PatEmpresa> GetByIdTallerFromPatEmpresaTaller(System.Int32 _idTaller)
		{
			int count = -1;
			return GetByIdTallerFromPatEmpresaTaller(null,_idTaller, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets PAT.Entities.PatEmpresa objects from the datasource by ID_TALLER in the
		///		PAT_EMPRESA_TALLER table. Table PAT_EMPRESA is related to table PAT_TALLER
		///		through the (M:N) relationship defined in the PAT_EMPRESA_TALLER table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_idTaller"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of PatEmpresa objects.</returns>
		public TList<PatEmpresa> GetByIdTallerFromPatEmpresaTaller(System.Int32 _idTaller, int start, int pageLength)
		{
			int count = -1;
			return GetByIdTallerFromPatEmpresaTaller(null, _idTaller, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets PatEmpresa objects from the datasource by ID_TALLER in the
		///		PAT_EMPRESA_TALLER table. Table PAT_EMPRESA is related to table PAT_TALLER
		///		through the (M:N) relationship defined in the PAT_EMPRESA_TALLER table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idTaller"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of PAT_EMPRESA objects.</returns>
		public TList<PatEmpresa> GetByIdTallerFromPatEmpresaTaller(TransactionManager transactionManager, System.Int32 _idTaller)
		{
			int count = -1;
			return GetByIdTallerFromPatEmpresaTaller(transactionManager, _idTaller, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets PatEmpresa objects from the datasource by ID_TALLER in the
		///		PAT_EMPRESA_TALLER table. Table PAT_EMPRESA is related to table PAT_TALLER
		///		through the (M:N) relationship defined in the PAT_EMPRESA_TALLER table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idTaller"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of PAT_EMPRESA objects.</returns>
		public TList<PatEmpresa> GetByIdTallerFromPatEmpresaTaller(TransactionManager transactionManager, System.Int32 _idTaller,int start, int pageLength)
		{
			int count = -1;
			return GetByIdTallerFromPatEmpresaTaller(transactionManager, _idTaller, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets PatEmpresa objects from the datasource by ID_TALLER in the
		///		PAT_EMPRESA_TALLER table. Table PAT_EMPRESA is related to table PAT_TALLER
		///		through the (M:N) relationship defined in the PAT_EMPRESA_TALLER table.
		/// </summary>
		/// <param name="_idTaller"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of PAT_EMPRESA objects.</returns>
		public TList<PatEmpresa> GetByIdTallerFromPatEmpresaTaller(System.Int32 _idTaller,int start, int pageLength, out int count)
		{
			
			return GetByIdTallerFromPatEmpresaTaller(null, _idTaller, start, pageLength, out count);
		}


		/// <summary>
		///		Gets PAT_EMPRESA objects from the datasource by ID_TALLER in the
		///		PAT_EMPRESA_TALLER table. Table PAT_EMPRESA is related to table PAT_TALLER
		///		through the (M:N) relationship defined in the PAT_EMPRESA_TALLER table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="_idTaller"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of PatEmpresa objects.</returns>
		public abstract TList<PatEmpresa> GetByIdTallerFromPatEmpresaTaller(TransactionManager transactionManager,System.Int32 _idTaller, int start, int pageLength, out int count);
		
		#endregion GetByIdTallerFromPatEmpresaTaller
		
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, PAT.Entities.PatEmpresaKey key)
		{
			return Delete(transactionManager, key.IdEmpresa);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_idEmpresa">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Int32 _idEmpresa)
		{
			return Delete(null, _idEmpresa);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idEmpresa">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Int32 _idEmpresa);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PAT_EMPRESA_PAT_UBICACION_GEOGRAFICA key.
		///		FK_PAT_EMPRESA_PAT_UBICACION_GEOGRAFICA Description: 
		/// </summary>
		/// <param name="_idUbicacionPais"></param>
		/// <returns>Returns a typed collection of PAT.Entities.PatEmpresa objects.</returns>
		public TList<PatEmpresa> GetByIdUbicacionPais(System.Int32 _idUbicacionPais)
		{
			int count = -1;
			return GetByIdUbicacionPais(_idUbicacionPais, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PAT_EMPRESA_PAT_UBICACION_GEOGRAFICA key.
		///		FK_PAT_EMPRESA_PAT_UBICACION_GEOGRAFICA Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idUbicacionPais"></param>
		/// <returns>Returns a typed collection of PAT.Entities.PatEmpresa objects.</returns>
		/// <remarks></remarks>
		public TList<PatEmpresa> GetByIdUbicacionPais(TransactionManager transactionManager, System.Int32 _idUbicacionPais)
		{
			int count = -1;
			return GetByIdUbicacionPais(transactionManager, _idUbicacionPais, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_PAT_EMPRESA_PAT_UBICACION_GEOGRAFICA key.
		///		FK_PAT_EMPRESA_PAT_UBICACION_GEOGRAFICA Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idUbicacionPais"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of PAT.Entities.PatEmpresa objects.</returns>
		public TList<PatEmpresa> GetByIdUbicacionPais(TransactionManager transactionManager, System.Int32 _idUbicacionPais, int start, int pageLength)
		{
			int count = -1;
			return GetByIdUbicacionPais(transactionManager, _idUbicacionPais, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PAT_EMPRESA_PAT_UBICACION_GEOGRAFICA key.
		///		fkPatEmpresaPatUbicacionGeografica Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_idUbicacionPais"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of PAT.Entities.PatEmpresa objects.</returns>
		public TList<PatEmpresa> GetByIdUbicacionPais(System.Int32 _idUbicacionPais, int start, int pageLength)
		{
			int count =  -1;
			return GetByIdUbicacionPais(null, _idUbicacionPais, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PAT_EMPRESA_PAT_UBICACION_GEOGRAFICA key.
		///		fkPatEmpresaPatUbicacionGeografica Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_idUbicacionPais"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of PAT.Entities.PatEmpresa objects.</returns>
		public TList<PatEmpresa> GetByIdUbicacionPais(System.Int32 _idUbicacionPais, int start, int pageLength,out int count)
		{
			return GetByIdUbicacionPais(null, _idUbicacionPais, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PAT_EMPRESA_PAT_UBICACION_GEOGRAFICA key.
		///		FK_PAT_EMPRESA_PAT_UBICACION_GEOGRAFICA Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idUbicacionPais"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of PAT.Entities.PatEmpresa objects.</returns>
		public abstract TList<PatEmpresa> GetByIdUbicacionPais(TransactionManager transactionManager, System.Int32 _idUbicacionPais, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override PAT.Entities.PatEmpresa Get(TransactionManager transactionManager, PAT.Entities.PatEmpresaKey key, int start, int pageLength)
		{
			return GetByIdEmpresa(transactionManager, key.IdEmpresa, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_PAT_EMPRESA index.
		/// </summary>
		/// <param name="_idEmpresa"></param>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatEmpresa"/> class.</returns>
		public PAT.Entities.PatEmpresa GetByIdEmpresa(System.Int32 _idEmpresa)
		{
			int count = -1;
			return GetByIdEmpresa(null,_idEmpresa, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PAT_EMPRESA index.
		/// </summary>
		/// <param name="_idEmpresa"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatEmpresa"/> class.</returns>
		public PAT.Entities.PatEmpresa GetByIdEmpresa(System.Int32 _idEmpresa, int start, int pageLength)
		{
			int count = -1;
			return GetByIdEmpresa(null, _idEmpresa, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PAT_EMPRESA index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idEmpresa"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatEmpresa"/> class.</returns>
		public PAT.Entities.PatEmpresa GetByIdEmpresa(TransactionManager transactionManager, System.Int32 _idEmpresa)
		{
			int count = -1;
			return GetByIdEmpresa(transactionManager, _idEmpresa, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PAT_EMPRESA index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idEmpresa"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatEmpresa"/> class.</returns>
		public PAT.Entities.PatEmpresa GetByIdEmpresa(TransactionManager transactionManager, System.Int32 _idEmpresa, int start, int pageLength)
		{
			int count = -1;
			return GetByIdEmpresa(transactionManager, _idEmpresa, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PAT_EMPRESA index.
		/// </summary>
		/// <param name="_idEmpresa"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatEmpresa"/> class.</returns>
		public PAT.Entities.PatEmpresa GetByIdEmpresa(System.Int32 _idEmpresa, int start, int pageLength, out int count)
		{
			return GetByIdEmpresa(null, _idEmpresa, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PAT_EMPRESA index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idEmpresa"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatEmpresa"/> class.</returns>
		public abstract PAT.Entities.PatEmpresa GetByIdEmpresa(TransactionManager transactionManager, System.Int32 _idEmpresa, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;PatEmpresa&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;PatEmpresa&gt;"/></returns>
		public static TList<PatEmpresa> Fill(IDataReader reader, TList<PatEmpresa> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				PAT.Entities.PatEmpresa c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("PatEmpresa")
					.Append("|").Append((System.Int32)reader[((int)PatEmpresaColumn.IdEmpresa - 1)]).ToString();
					c = EntityManager.LocateOrCreate<PatEmpresa>(
					key.ToString(), // EntityTrackingKey
					"PatEmpresa",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new PAT.Entities.PatEmpresa();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.IdEmpresa = (System.Int32)reader[((int)PatEmpresaColumn.IdEmpresa - 1)];
					c.IdTipoIdentificacion = (System.Int16)reader[((int)PatEmpresaColumn.IdTipoIdentificacion - 1)];
					c.NumeroIdentificacion = (System.String)reader[((int)PatEmpresaColumn.NumeroIdentificacion - 1)];
					c.DigitoVerificacion = (System.String)reader[((int)PatEmpresaColumn.DigitoVerificacion - 1)];
					c.RazonSocial = (System.String)reader[((int)PatEmpresaColumn.RazonSocial - 1)];
					c.Sigla = (System.String)reader[((int)PatEmpresaColumn.Sigla - 1)];
					c.NombreComercial = (System.String)reader[((int)PatEmpresaColumn.NombreComercial - 1)];
					c.IdUbicacionPais = (System.Int32)reader[((int)PatEmpresaColumn.IdUbicacionPais - 1)];
					c.IdRegimen = (System.Int16)reader[((int)PatEmpresaColumn.IdRegimen - 1)];
					c.Direccion = (System.String)reader[((int)PatEmpresaColumn.Direccion - 1)];
					c.TelefonoFijo = (System.String)reader[((int)PatEmpresaColumn.TelefonoFijo - 1)];
					c.EMail = (System.String)reader[((int)PatEmpresaColumn.EMail - 1)];
					c.Activo = (System.Boolean)reader[((int)PatEmpresaColumn.Activo - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="PAT.Entities.PatEmpresa"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="PAT.Entities.PatEmpresa"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, PAT.Entities.PatEmpresa entity)
		{
			if (!reader.Read()) return;
			
			entity.IdEmpresa = (System.Int32)reader[((int)PatEmpresaColumn.IdEmpresa - 1)];
			entity.IdTipoIdentificacion = (System.Int16)reader[((int)PatEmpresaColumn.IdTipoIdentificacion - 1)];
			entity.NumeroIdentificacion = (System.String)reader[((int)PatEmpresaColumn.NumeroIdentificacion - 1)];
			entity.DigitoVerificacion = (System.String)reader[((int)PatEmpresaColumn.DigitoVerificacion - 1)];
			entity.RazonSocial = (System.String)reader[((int)PatEmpresaColumn.RazonSocial - 1)];
			entity.Sigla = (System.String)reader[((int)PatEmpresaColumn.Sigla - 1)];
			entity.NombreComercial = (System.String)reader[((int)PatEmpresaColumn.NombreComercial - 1)];
			entity.IdUbicacionPais = (System.Int32)reader[((int)PatEmpresaColumn.IdUbicacionPais - 1)];
			entity.IdRegimen = (System.Int16)reader[((int)PatEmpresaColumn.IdRegimen - 1)];
			entity.Direccion = (System.String)reader[((int)PatEmpresaColumn.Direccion - 1)];
			entity.TelefonoFijo = (System.String)reader[((int)PatEmpresaColumn.TelefonoFijo - 1)];
			entity.EMail = (System.String)reader[((int)PatEmpresaColumn.EMail - 1)];
			entity.Activo = (System.Boolean)reader[((int)PatEmpresaColumn.Activo - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="PAT.Entities.PatEmpresa"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="PAT.Entities.PatEmpresa"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, PAT.Entities.PatEmpresa entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.IdEmpresa = (System.Int32)dataRow["ID_EMPRESA"];
			entity.IdTipoIdentificacion = (System.Int16)dataRow["ID_TIPO_IDENTIFICACION"];
			entity.NumeroIdentificacion = (System.String)dataRow["NUMERO_IDENTIFICACION"];
			entity.DigitoVerificacion = (System.String)dataRow["DIGITO_VERIFICACION"];
			entity.RazonSocial = (System.String)dataRow["RAZON_SOCIAL"];
			entity.Sigla = (System.String)dataRow["SIGLA"];
			entity.NombreComercial = (System.String)dataRow["NOMBRE_COMERCIAL"];
			entity.IdUbicacionPais = (System.Int32)dataRow["ID_UBICACION_PAIS"];
			entity.IdRegimen = (System.Int16)dataRow["ID_REGIMEN"];
			entity.Direccion = (System.String)dataRow["DIRECCION"];
			entity.TelefonoFijo = (System.String)dataRow["TELEFONO_FIJO"];
			entity.EMail = (System.String)dataRow["E_MAIL"];
			entity.Activo = (System.Boolean)dataRow["ACTIVO"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="PAT.Entities.PatEmpresa"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">PAT.Entities.PatEmpresa Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, PAT.Entities.PatEmpresa entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region IdUbicacionPaisSource	
			if (CanDeepLoad(entity, "PatUbicacionGeografica|IdUbicacionPaisSource", deepLoadType, innerList) 
				&& entity.IdUbicacionPaisSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.IdUbicacionPais;
				PatUbicacionGeografica tmpEntity = EntityManager.LocateEntity<PatUbicacionGeografica>(EntityLocator.ConstructKeyFromPkItems(typeof(PatUbicacionGeografica), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.IdUbicacionPaisSource = tmpEntity;
				else
					entity.IdUbicacionPaisSource = DataRepository.PatUbicacionGeograficaProvider.GetByIdUbicacion(transactionManager, entity.IdUbicacionPais);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'IdUbicacionPaisSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.IdUbicacionPaisSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.PatUbicacionGeograficaProvider.DeepLoad(transactionManager, entity.IdUbicacionPaisSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion IdUbicacionPaisSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByIdEmpresa methods when available
			
			#region PatEmpresaTallerCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<PatEmpresaTaller>|PatEmpresaTallerCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'PatEmpresaTallerCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.PatEmpresaTallerCollection = DataRepository.PatEmpresaTallerProvider.GetByIdEmpresa(transactionManager, entity.IdEmpresa);

				if (deep && entity.PatEmpresaTallerCollection.Count > 0)
				{
					deepHandles.Add("PatEmpresaTallerCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<PatEmpresaTaller>) DataRepository.PatEmpresaTallerProvider.DeepLoad,
						new object[] { transactionManager, entity.PatEmpresaTallerCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region IdTallerPatTallerCollection_From_PatEmpresaTaller
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<PatTaller>|IdTallerPatTallerCollection_From_PatEmpresaTaller", deepLoadType, innerList))
			{
				entity.IdTallerPatTallerCollection_From_PatEmpresaTaller = DataRepository.PatTallerProvider.GetByIdEmpresaFromPatEmpresaTaller(transactionManager, entity.IdEmpresa);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'IdTallerPatTallerCollection_From_PatEmpresaTaller' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.IdTallerPatTallerCollection_From_PatEmpresaTaller != null)
				{
					deepHandles.Add("IdTallerPatTallerCollection_From_PatEmpresaTaller",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< PatTaller >) DataRepository.PatTallerProvider.DeepLoad,
						new object[] { transactionManager, entity.IdTallerPatTallerCollection_From_PatEmpresaTaller, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			#region PatUsuarioCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<PatUsuario>|PatUsuarioCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'PatUsuarioCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.PatUsuarioCollection = DataRepository.PatUsuarioProvider.GetByIdEmpresa(transactionManager, entity.IdEmpresa);

				if (deep && entity.PatUsuarioCollection.Count > 0)
				{
					deepHandles.Add("PatUsuarioCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<PatUsuario>) DataRepository.PatUsuarioProvider.DeepLoad,
						new object[] { transactionManager, entity.PatUsuarioCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the PAT.Entities.PatEmpresa object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">PAT.Entities.PatEmpresa instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">PAT.Entities.PatEmpresa Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, PAT.Entities.PatEmpresa entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region IdUbicacionPaisSource
			if (CanDeepSave(entity, "PatUbicacionGeografica|IdUbicacionPaisSource", deepSaveType, innerList) 
				&& entity.IdUbicacionPaisSource != null)
			{
				DataRepository.PatUbicacionGeograficaProvider.Save(transactionManager, entity.IdUbicacionPaisSource);
				entity.IdUbicacionPais = entity.IdUbicacionPaisSource.IdUbicacion;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();

			#region IdTallerPatTallerCollection_From_PatEmpresaTaller>
			if (CanDeepSave(entity.IdTallerPatTallerCollection_From_PatEmpresaTaller, "List<PatTaller>|IdTallerPatTallerCollection_From_PatEmpresaTaller", deepSaveType, innerList))
			{
				if (entity.IdTallerPatTallerCollection_From_PatEmpresaTaller.Count > 0 || entity.IdTallerPatTallerCollection_From_PatEmpresaTaller.DeletedItems.Count > 0)
				{
					DataRepository.PatTallerProvider.Save(transactionManager, entity.IdTallerPatTallerCollection_From_PatEmpresaTaller); 
					deepHandles.Add("IdTallerPatTallerCollection_From_PatEmpresaTaller",
						new KeyValuePair<Delegate, object>((DeepSaveHandle<PatTaller>) DataRepository.PatTallerProvider.DeepSave,
						new object[] { transactionManager, entity.IdTallerPatTallerCollection_From_PatEmpresaTaller, deepSaveType, childTypes, innerList }
					));
				}
			}
			#endregion 
	
			#region List<PatEmpresaTaller>
				if (CanDeepSave(entity.PatEmpresaTallerCollection, "List<PatEmpresaTaller>|PatEmpresaTallerCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(PatEmpresaTaller child in entity.PatEmpresaTallerCollection)
					{
						if(child.IdEmpresaSource != null)
						{
								child.IdEmpresa = child.IdEmpresaSource.IdEmpresa;
						}

						if(child.IdTallerSource != null)
						{
								child.IdTaller = child.IdTallerSource.IdTaller;
						}

					}

					if (entity.PatEmpresaTallerCollection.Count > 0 || entity.PatEmpresaTallerCollection.DeletedItems.Count > 0)
					{
						//DataRepository.PatEmpresaTallerProvider.Save(transactionManager, entity.PatEmpresaTallerCollection);
						
						deepHandles.Add("PatEmpresaTallerCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< PatEmpresaTaller >) DataRepository.PatEmpresaTallerProvider.DeepSave,
							new object[] { transactionManager, entity.PatEmpresaTallerCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
	
			#region List<PatUsuario>
				if (CanDeepSave(entity.PatUsuarioCollection, "List<PatUsuario>|PatUsuarioCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(PatUsuario child in entity.PatUsuarioCollection)
					{
						if(child.IdEmpresaSource != null)
						{
							child.IdEmpresa = child.IdEmpresaSource.IdEmpresa;
						}
						else
						{
							child.IdEmpresa = entity.IdEmpresa;
						}

					}

					if (entity.PatUsuarioCollection.Count > 0 || entity.PatUsuarioCollection.DeletedItems.Count > 0)
					{
						//DataRepository.PatUsuarioProvider.Save(transactionManager, entity.PatUsuarioCollection);
						
						deepHandles.Add("PatUsuarioCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< PatUsuario >) DataRepository.PatUsuarioProvider.DeepSave,
							new object[] { transactionManager, entity.PatUsuarioCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region PatEmpresaChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>PAT.Entities.PatEmpresa</c>
	///</summary>
	public enum PatEmpresaChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>PatUbicacionGeografica</c> at IdUbicacionPaisSource
		///</summary>
		[ChildEntityType(typeof(PatUbicacionGeografica))]
		PatUbicacionGeografica,
	
		///<summary>
		/// Collection of <c>PatEmpresa</c> as OneToMany for PatEmpresaTallerCollection
		///</summary>
		[ChildEntityType(typeof(TList<PatEmpresaTaller>))]
		PatEmpresaTallerCollection,

		///<summary>
		/// Collection of <c>PatEmpresa</c> as ManyToMany for PatTallerCollection_From_PatEmpresaTaller
		///</summary>
		[ChildEntityType(typeof(TList<PatTaller>))]
		IdTallerPatTallerCollection_From_PatEmpresaTaller,

		///<summary>
		/// Collection of <c>PatEmpresa</c> as OneToMany for PatUsuarioCollection
		///</summary>
		[ChildEntityType(typeof(TList<PatUsuario>))]
		PatUsuarioCollection,
	}
	
	#endregion PatEmpresaChildEntityTypes
	
	#region PatEmpresaFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;PatEmpresaColumn&gt;"/> class
	/// that is used exclusively with a <see cref="PatEmpresa"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class PatEmpresaFilterBuilder : SqlFilterBuilder<PatEmpresaColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the PatEmpresaFilterBuilder class.
		/// </summary>
		public PatEmpresaFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the PatEmpresaFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public PatEmpresaFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the PatEmpresaFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public PatEmpresaFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion PatEmpresaFilterBuilder
	
	#region PatEmpresaParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;PatEmpresaColumn&gt;"/> class
	/// that is used exclusively with a <see cref="PatEmpresa"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class PatEmpresaParameterBuilder : ParameterizedSqlFilterBuilder<PatEmpresaColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the PatEmpresaParameterBuilder class.
		/// </summary>
		public PatEmpresaParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the PatEmpresaParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public PatEmpresaParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the PatEmpresaParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public PatEmpresaParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion PatEmpresaParameterBuilder
	
	#region PatEmpresaSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;PatEmpresaColumn&gt;"/> class
	/// that is used exclusively with a <see cref="PatEmpresa"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class PatEmpresaSortBuilder : SqlSortBuilder<PatEmpresaColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the PatEmpresaSqlSortBuilder class.
		/// </summary>
		public PatEmpresaSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion PatEmpresaSortBuilder
	
} // end namespace
